package com.ruoyi.web.controller.ring.app;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.TimeUnitEnums;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ring.domain.RingSleepDetail;
import com.ruoyi.ring.domain.dto.RingSleepDetailInfoDTO;
import com.ruoyi.ring.domain.dto.RingSleepDetailListDTO;
import com.ruoyi.ring.domain.dto.SleepInfoDTO;
import com.ruoyi.ring.domain.dto.VerifyQueryDTO;
import com.ruoyi.ring.domain.vo.*;
import com.ruoyi.ring.service.IRingSleepDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 睡眠首页 （APP）
 */
@Slf4j
@RestController
@RequestMapping("/ring/app/sleep/home")
public class AppSleepHomeController extends BaseController {

    @Resource
    private IRingSleepDetailService ringSleepDetailService;
    /**
     * 查询日历数据列表-app
     */
    @GetMapping("/query-calendar-list")
    public AjaxResult queryCalendarList(RingSleepDetailInfoDTO sleepDetailInfoDTO){
        long userId = SecurityUtils.getUserId();
        log.info("查询日历数据列表: userId={}, param={}", userId, JSON.toJSONString(sleepDetailInfoDTO));
        return AjaxResult.success(ringSleepDetailService.queryCalendarList(sleepDetailInfoDTO));
    }
    /**
     * 查询睡眠详情列表-app
     */
    @GetMapping("/query-list")
    public AjaxResult queryList(RingSleepDetailListDTO sleepDetailListDTO){
        long userId = SecurityUtils.getUserId();
        log.info("查询睡眠详情列表: userId={}, param={}", userId, JSON.toJSONString(sleepDetailListDTO));
        List<RingSleepDetail> list = ringSleepDetailService.queryList(sleepDetailListDTO);
        return AjaxResult.success(list);
    }

    /**
     * 上报睡眠详情前验证-app
     */
    @GetMapping("/sleep-verify")
    public AjaxResult verify(@RequestBody VerifyQueryDTO queryDTO)
    {
        long userId = SecurityUtils.getUserId();
        log.info("上报睡眠详情前验证: userId={}, param={}", userId, JSON.toJSONString(queryDTO));
        return AjaxResult.success(ringSleepDetailService.verify(queryDTO));
    }
    /**
     * 上报睡眠详情-app
     */
    @Log(title = "上报睡眠详情", businessType = BusinessType.INSERT)
    @PostMapping("/add-detail")
    public AjaxResult addDetail(@RequestBody SleepInfoDTO sleepInfo)
    {
        sleepInfo.setDailyValue(DateUtils.dateTime(DateUtils.YYYY_MM_DD, DateUtils.dateTime(sleepInfo.getEndDate())));
        return toAjax(ringSleepDetailService.addDetail(sleepInfo));
    }

    /**
     * 查询睡眠内容详情-app 首页
     */
    @GetMapping("/query-info")
    public AjaxResult queryInfo(RingSleepDetailInfoDTO sleepDetailInfoDTO){
        long userId = SecurityUtils.getUserId();
        log.info("查询睡眠内容详情: userId={}, param={}", userId, JSON.toJSONString(sleepDetailInfoDTO));
        if(TimeUnitEnums.DAY.getCode().equals(sleepDetailInfoDTO.getType())){
            RingSleepDetailVO sleepDetailVO = ringSleepDetailService.queryDayInfo(sleepDetailInfoDTO);
            return AjaxResult.success(sleepDetailVO);
        }else if(TimeUnitEnums.WEEK.getCode().equals(sleepDetailInfoDTO.getType()) || TimeUnitEnums.MONTH.getCode().equals(sleepDetailInfoDTO.getType())){
            return AjaxResult.success(ringSleepDetailService.queryWeekOrMonthInfo(sleepDetailInfoDTO));
        }
        return AjaxResult.success();
    }
    /**
     * 睡眠质量评分-app 首页
     */
    @GetMapping("/query-quality-score")
    public AjaxResult queryQualityScore(RingSleepDetailInfoDTO sleepDetailInfoDTO){
        long userId = SecurityUtils.getUserId();
        log.info("睡眠质量评分: userId={}, param={}", userId, JSON.toJSONString(sleepDetailInfoDTO));
        RingSleepQualityScoreVO qualityScoreVO = ringSleepDetailService.queryQualityScore(sleepDetailInfoDTO);
        return AjaxResult.success(qualityScoreVO);
    }
    /**
     * 睡眠比例-app 首页
     */
    @GetMapping("/query-proportion")
    public AjaxResult queryProportion(RingSleepDetailInfoDTO sleepDetailInfoDTO){
        long userId = SecurityUtils.getUserId();
        log.info("睡眠比例: userId={}, param={}", userId, JSON.toJSONString(sleepDetailInfoDTO));
        Map<Integer, RingSleepProportionVO> map = ringSleepDetailService.queryProportion(sleepDetailInfoDTO);
        return AjaxResult.success(map);
    }
    /**
     * 小睡-app 首页
     */
    @GetMapping("/query-short-sleep")
    public AjaxResult queryShortSleep(RingSleepDetailInfoDTO sleepDetailInfoDTO){
        long userId = SecurityUtils.getUserId();
        log.info("小睡: userId={}, param={}", userId, JSON.toJSONString(sleepDetailInfoDTO));
        return AjaxResult.success(ringSleepDetailService.queryShortSleep(sleepDetailInfoDTO));
    }

    /**
     * 平均睡眠心率-app 首页
     */
    @GetMapping("/query-sleep-heart-rate")
    public AjaxResult querySleepHeartRate(RingSleepDetailInfoDTO sleepDetailInfoDTO){
        long userId = SecurityUtils.getUserId();
        log.info("平均睡眠心率: userId={}, param={}", userId, JSON.toJSONString(sleepDetailInfoDTO));
        SleepHomeRateVO heartRateVO = ringSleepDetailService.queryLongSleepHeartRate(sleepDetailInfoDTO);
        return AjaxResult.success(heartRateVO);
    }
    /**
     * 平均心率变异性-app 首页
     */
    @GetMapping("/query-sleep-hrv")
    public AjaxResult querySleepHrv(RingSleepDetailInfoDTO sleepDetailInfoDTO){
        long userId = SecurityUtils.getUserId();
        log.info("平均心率变异性: userId={}, param={}", userId, JSON.toJSONString(sleepDetailInfoDTO));
        SleepHomeRateVO heartRateVO = ringSleepDetailService.querySleepHrv(sleepDetailInfoDTO);
        return AjaxResult.success(heartRateVO);
    }

    /**
     * 血氧饱和度-app 首页
     */
    @GetMapping("/query-sleep-blood-oxygen")
    public AjaxResult querySleepBloodOxygen(RingSleepDetailInfoDTO sleepDetailInfoDTO){
        long userId = SecurityUtils.getUserId();
        log.info("血氧饱和度: userId={}, param={}", userId, JSON.toJSONString(sleepDetailInfoDTO));
        return AjaxResult.success(ringSleepDetailService.querySleepBloodOxygen(sleepDetailInfoDTO));
    }

    /**
     * 睡眠活动状态-app 首页
     */
    @GetMapping("/query-sleep-activity-status")
    public AjaxResult querySleepActivityStatus(RingSleepDetailInfoDTO sleepDetailInfoDTO){
        long userId = SecurityUtils.getUserId();
        log.info("睡眠活动状态: userId={}, param={}", userId, JSON.toJSONString(sleepDetailInfoDTO));
        return AjaxResult.success(ringSleepDetailService.querySleepActivityStatus(sleepDetailInfoDTO));
    }
    /**
     * 近7天睡眠趋势-app 首页
     */
    @GetMapping("/query-sleep-trend")
    public AjaxResult querySleepTrend(RingSleepDetailInfoDTO sleepDetailInfoDTO){
        long userId = SecurityUtils.getUserId();
        log.info("近7天睡眠趋势: userId={}, param={}", userId, JSON.toJSONString(sleepDetailInfoDTO));
        if(TimeUnitEnums.DAY.getCode().equals(sleepDetailInfoDTO.getType())){
            return AjaxResult.success(ringSleepDetailService.querySleepTrend(sleepDetailInfoDTO));
        }else {
            return AjaxResult.success(ringSleepDetailService.querySleepTrendWeekOrMonth(sleepDetailInfoDTO));
        }
    }

}
